package com.leiqichao.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leiqichao.backend.common.Page;
import com.leiqichao.backend.entity.Article;
import com.leiqichao.backend.enums.ArticleStatus;
import com.leiqichao.backend.mapper.ArticleMapper;
import com.leiqichao.backend.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<Article> getAllArticles() {
        List<Article> articles = articleMapper.selectAllArticles();
        for (Article article : articles) {
            ArticleStatus status = ArticleStatus.valueOf(article.getStatus().toUpperCase());
            article.setStatus(status.getDescription());
        }
        return articles;
    }

    @Override
    public Article getArticleById(Long id) {
        Article article = articleMapper.selectArticleById(id);
        //根据状态枚举值，返回对应的枚举对象
        if (article != null) {
            ArticleStatus status = ArticleStatus.valueOf(article.getStatus().toUpperCase());
            article.setStatus(status.getDescription());
        }
        return article;
    }

    @Override
    public boolean createArticle(Article article) {
        return articleMapper.insertArticle(article) > 0;
    }

    @Override
    public boolean updateArticle(Article article) {
        return articleMapper.updateArticle(article) > 0;
    }

    @Override
    public boolean deleteArticle(Long id) {
        return articleMapper.deleteArticleById(id) > 0;
    }

    @Override
    public Page<Article> page(int pageNum, int pageSize,Article article) {
        // 创建分页对象
        Page<Article> page = new Page<>(pageNum, pageSize);


        // 动态构建查询条件
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(false,false,"created_at");
        if (article != null) {
            // 如果 article 的字段不为空，则动态添加到查询条件中
            if (article.getTitle() != null) {
                queryWrapper.like("title", article.getTitle()); // 模糊查询标题
            }
            if (article.getStatus() != null) {
                queryWrapper.eq("status", article.getStatus()); // 匹配状态
            }
        }
        // 查询总记录数
        Long total = articleMapper.selectCount(queryWrapper);
        page.setTotal(Math.toIntExact(total));

        // 查询当前页的数据
        Page<Article> articlePage = articleMapper.selectPage(page, queryWrapper);

        // 转换文章状态为描述信息
        for (Article articles : articlePage.getRecords()) {
            ArticleStatus status = ArticleStatus.valueOf(articles.getStatus().toUpperCase());
            articles.setStatus(status.getDescription());
        }

        return page;
    }

    /**
     * 更新文章状态
     * @param articleId
     * @param newStatus
     * @return
     */
    @Override
    public boolean updateArticleStatus(Long articleId, String newStatus) {
        // 检查文章ID是否为空
        if (articleId == null) {
            throw new IllegalArgumentException("Article ID cannot be null");
        }
        
        // 检查新状态是否为空
        if (newStatus == null || newStatus.isEmpty()) {
            throw new IllegalArgumentException("New status cannot be null or empty");
        }
        
        // 根据文章ID查找文章
        Article article = articleMapper.selectArticleById(articleId);
        if (article == null) {
            throw new RuntimeException("Article with ID " + articleId + " not found");
        }
        
        // 更新文章状态
        article.setStatus(newStatus);
        
        // 保存更新后的文章
        int i = articleMapper.updateArticle(article);
        if (i == 0) {
            throw new RuntimeException("Failed to update article status");
        }
        return true;
    }

    @Override
    public boolean pushArticle(Long id) {
        if (id != null) {
            Article article = articleMapper.selectArticleById(id);
            if (article != null) {
                article.setStatus("published");
                article.setPublishedAt(LocalDateTime.now());
                return articleMapper.updateArticle(article) > 0;
            }
        }
        return false;
    }
}